package de.urlaub.client;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import de.urlaub.client.data.Employee;
import de.urlaub.client.gui.LoginDialog;
import de.urlaub.client.gui.RespondPanel;
import de.urlaub.client.gui.SoonVacationPanel;
import de.urlaub.client.gui.StartPanel;
import de.urlaub.client.gui.VacationRequestPanel;
import de.urlaub.client.gui.ViewChooserListener;
import de.urlaub.server.comm.HornetQSender;
import de.urlaub.server.logic.DataUtil;
import de.urlaub.server.logic.DataUtilRemote;

public class VacationClient 
{	
	private static boolean sampleDataCreated;
	private DataUtilRemote dataUtil;
	private Employee currentUser;
	
	public static HashMap<String, JFrame> employeeFrames = new HashMap<String, JFrame>();
	private JFrame mainFrame;
	
	private ArrayList<JPanel> contentPanels;
	private JPanel currentContentPanel;
	private JComboBox viewChooser;
	
	public VacationClient()
	{
		this.dataUtil = new DataUtil();		
		try
		{
			Context context = getInitialContext();
			this.dataUtil = (DataUtilRemote) context.lookup("DataUtil/remote");
		}
		catch (NamingException nEx)
		{
			nEx.printStackTrace();
		}
		
		if (!sampleDataCreated)
		{
			sampleDataCreated = true;
			this.dataUtil.createSampleData();
		}
		
		LoginDialog loginDialog = new LoginDialog(this.dataUtil.getEmployees(), this);
		loginDialog.setVisible(true);
	}
	
	private static InitialContext getInitialContext()
	{
		InitialContext initialContext = null;
		
		Hashtable<String, String> props = new Hashtable<String, String>();
		
		props.put(InitialContext.INITIAL_CONTEXT_FACTORY,
				"org.jnp.interfaces.NamingContextFactory");
		props.put(InitialContext.PROVIDER_URL, "jnp://127.0.0.1:1099");
		
		try
		{
			initialContext = new InitialContext (props);
		}
		catch (NamingException nEx)
		{
			nEx.printStackTrace();
		}
		
		return initialContext;
	}
	
	public void startProgramm(Employee employee)
	{
		this.currentUser = employee;
		
		initMainFrame();
		initContentPanels();
		initViewChooser();
		
		this.mainFrame.setVisible(true);
		refreshFrame();
		new HornetQSender().sendMessage(this.currentUser, "Willkommen " + this.currentUser.getEmployeeId());
	}
	
	private void initMainFrame()
	{
		this.mainFrame = new JFrame("Urlaubsplanung");
		this.mainFrame.setPreferredSize(new Dimension(800, 600));
		this.mainFrame.setResizable(false);
		this.mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		employeeFrames.put(this.currentUser.getEmployeeId(), this.mainFrame);
	}
	
	private void initContentPanels()
	{
		this.contentPanels = new ArrayList<JPanel>();
		this.contentPanels.add(new StartPanel(this.dataUtil));
		this.contentPanels.add(new VacationRequestPanel(this.dataUtil, this.currentUser));
		this.contentPanels.add(new SoonVacationPanel(this.dataUtil));
		if (this.currentUser.getDivision().isCanApprove())
			this.contentPanels.add(new RespondPanel(this.dataUtil));
	}

	private void initViewChooser()
	{
		this.viewChooser = new JComboBox(this.contentPanels.toArray());
		this.viewChooser.addActionListener(new ViewChooserListener(this));
		this.mainFrame.add(this.viewChooser, BorderLayout.NORTH);
	}
	
	public void refreshFrame()
	{
		if (this.currentContentPanel != null)
			this.mainFrame.remove(this.currentContentPanel);
		
		setCurrentContentPanel();

		this.mainFrame.pack();
	}
	
	private void setCurrentContentPanel()
	{
		this.currentContentPanel = (JPanel) this.viewChooser.getSelectedItem();
		if (this.currentContentPanel instanceof StartPanel)
		{
			((StartPanel)this.currentContentPanel).buildPanel();
		}
		else if (this.currentContentPanel instanceof VacationRequestPanel)
		{
			((VacationRequestPanel)this.currentContentPanel).buildPanel();
		}
		else if (this.currentContentPanel instanceof SoonVacationPanel)
		{
			((SoonVacationPanel)this.currentContentPanel).buildPanel();
		}
		else // RespondPanel
		{
			((RespondPanel)this.currentContentPanel).buildPanel();
		}
		
		this.mainFrame.add(this.currentContentPanel, BorderLayout.CENTER);
	}
	
	public static void showMessageDialog(String employeeId, String message)
	{
		JOptionPane.showMessageDialog(employeeFrames.get(employeeId), message);
	}
}
